function Game(el){
    //保存地图对象
    this.el = document.querySelector(el)
    //console.log(this.el)
    
    //设置地图
    //将地图标签对象传递给map构造函数
    this.map = new Map(this.el)
    this.map.create()
    
    //设置食物
    //将地图标签对象传递给food构造函数，后期追加食物
    this.food = new Food(this.el)
    this.food.create()

    //绘制蛇
    this.snake = new Snake(this.el)
    this.snake.create()

    //设置定时器唯一标识
    let t = null
}

Game.prototype.start = function(){
    t = setInterval(()=>{
        //蛇移动
        this.snake.move()
        //判断蛇吃食
        let snakeX = this.snake.snakeObj[0].x * 20
        let snakeY = this.snake.snakeObj[0].y * 20
        let foodX = this.food.foodLeft
        let foodY = this.food.foodTop
        if (snakeX == foodX && snakeY == foodY) {
            let last = this.snake.snakeObj[this.snake.snakeObj.length - 1]
            this.snake.snakeObj.push({
                x:last.x,
                y:last.y,
                color:last.color,
                el:null
            })
            this.snake.create() //重新绘制蛇
            // 食物移动
            this.food.move()
        }
    },300)
    document.addEventListener('keydown',(e)=>{
        e = e || window.event
        console.log(e.keyCode)
        //获取键盘箭头按钮
        switch (e.keyCode) {
            case 37:
                if(this.snake.direction == 'right'){
                    clearInterval(t)
                    alert('撞到自己，游戏结束')
                }
                this.snake.direction = 'left'
                break;
            case 38:
                if(this.snake.direction == 'bottom'){
                    clearInterval(t)
                    alert('撞到自己，游戏结束')
                }
                this.snake.direction = 'top'
                break;
            case 39:
                if(this.snake.direction == 'left'){
                    clearInterval(t)
                    alert('撞到自己，游戏结束')
                }
                this.snake.direction = 'right'
                break;
            case 40:
                if(this.snake.direction == 'top'){
                    clearInterval(t)
                    alert('撞到自己，游戏结束')
                }
                this.snake.direction = 'bottom'
                break;
            default:
                break;
        }
    })
}

Game.prototype.stop = function(){
    clearInterval(t)
}